<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
     function ajax() {
        return new Promise((resolve, reject) => {
          setTimeout(() => {
            resolve("success--------1--");
          }, 1000);
        });
      }
      function ajax2() {
        return new Promise((resolve, reject) => {
          setTimeout(() => {
            reject ("success--------2--");
            // resolve("success--------2--");
          }, 2000);
        });
      }
      function ajax3() {
        return new Promise((resolve, reject) => {
          setTimeout(() => {
            resolve("success--------3--");
          }, 3000);
        });
      }
      const arr = [ajax(), ajax2(), ajax3()];

 //1. all 方法  必须等待全部加载完成返回的是数组 但是一旦遇到reject会直接返回错误不会接着执行
      async function result() {
        const res = await Promise.all(arr);
        console.log(res,'all');
      }
      result();

      //race  谁快返回谁 遇到错误就返回错误 
      async function raceFn() {
        const res = await Promise.race(arr);
        console.log(res,'race');
      }
      raceFn();

      //   allSettled 跟all机制类似但是会返回错误跟成功的数据
      /*
      [
          { status: "fulfilled"
            value: "success--------1--"}，
          {
            
        reason: "success--------2--"
        status: "rejected"
          }
          ]
      */
      async function allSettledFn() {
        const res = await Promise.allSettled(arr);
        console.log(res,'allSettled');
      }
      allSettledFn();


      // any 方法 只要有一个成功就返回成功 不需要判断  success--------1-- any
      async function anyFn() {
        const res = await Promise.any(arr);
        console.log(res,'any');
      }
      anyFn();
    </script>
  </body>
</html>
